<?php
/**
 * Created by PhpStorm.
 * User: trnall
 * Date: 2019/8/24
 * Time: 10:56
 */

namespace wechat;


use think\Cache;
use think\Db;

class AccessToken
{
    private $_appid;
    private $_appsecret;


    public function __construct($appid = '', $appsecret = '')
    {
        $this->_appid = $appid;
        $this->_appsecret = $appsecret;
    }

    /**
     * 生成access_token【做智小邻时废弃】
     * @return bool|mixed
     */
    /*public function getAccessToken()
    {
        $res = Db::name('wechat_config')->order("id desc")->find();
        if ($res && $res['access_token'] && $res['expire_time'] > time()) {
            return $res['access_token'];
        } else {
            //生成新的access_token
            $appid = $this->_appid ? $this->_appid : $res['appid'];
            $appsecret = $this->_appsecret ? $this->_appsecret : $res['appsecret'];
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
            $data = http_request($url);
            if ($data) {
                $accessToken = $data['access_token'];
                if ($res) {
                    Db::name('wechat_config')->where(['id' => $res['id']])->update([
                        'access_token'  =>$accessToken,
                        'expire_time'   => time()+6500,
                        'update_time'   => time()
                    ]);
                } else {
                    Db::name('wechat_config')->insert([
                        'access_token'  =>$accessToken,
                        'expire_time'   => time()+6500,
                        'update_time'   => time()
                    ]);
                }
            } else {
                $accessToken = false;
            }
            return $accessToken;
        }
    }*/

    public function getAccessToken()
    {
        $accessToken = Cache::get('access_token', '');
        p($accessToken,'获取$accessToken');
        if ($accessToken) return $accessToken;
        //生成新的access_token
        $appid = $this->_appid;
        $appsecret = $this->_appsecret;
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
        $data = http_request($url);
        p($data,'重新生成accesstoken-url'.$url);
        if ($data && isset($data['access_token'])) {
            $accessToken = $data['access_token'];
            Cache::set('access_token', $accessToken, 240);
            return $accessToken;
        }
        return null;
        /*$res = Db::name('shop_access_token')->where('appid', $this->_appid)->order("id desc")->find();
        if ($res && $res['access_token'] && $res['expire_time'] > time()) {
            return $res['access_token'];
        } else {
            //生成新的access_token
            $appid = $this->_appid;
            $appsecret = $this->_appsecret;
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
            $data = http_request($url);
            if ($data && isset($data['access_token'])) {
                $accessToken = $data['access_token'];
                if ($res) {
                    Db::name('shop_access_token')->where(['id' => $res['id']])->update([
                        'appid' => $appid,
                        'access_token' => $accessToken,
                        'expire_time' => time() + 6500,
                        'update_time' => time()
                    ]);
                } else {
                    Db::name('shop_access_token')->insert([
                        'appid' => $appid,
                        'access_token' => $accessToken,
                        'expire_time' => time() + 6500,
                        'create_time' => time(),
                        'update_time' => time()
                    ]);
                }
            } else {
                $accessToken = false;
            }
            return $accessToken;
        }*/
    }
}